package com.tengju.bff.interfaces.app.csa.assembler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tengju.bff.interfaces.app.csa.vo.GenSecCustomerServiceGreetVO;
import com.tengju.bff.interfaces.app.csa.vo.GenSecCustomerServiceInfoVO;
import com.tengju.user.domain.model.csa.ClubCustomerServiceInfo;
import org.apache.commons.lang3.StringUtils;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class GenSecCustomerServiceInfoAssembler {

    public GenSecCustomerServiceInfoAssembler() {
    }

    public static GenSecCustomerServiceInfoVO clubCustomerServiceToVO(ClubCustomerServiceInfo clubCustomerServiceInfo) {
        if (clubCustomerServiceInfo == null) {
            return null;
        }
        GenSecCustomerServiceInfoVO genSecCustomerServiceInfoVO = new GenSecCustomerServiceInfoVO();
        genSecCustomerServiceInfoVO.setCsaName(clubCustomerServiceInfo.getCsaName());
        genSecCustomerServiceInfoVO.setCsaWechat(clubCustomerServiceInfo.getCsaWechat());
        genSecCustomerServiceInfoVO.setHeadImage(clubCustomerServiceInfo.getHeadImage());
        genSecCustomerServiceInfoVO.setServiceWorkTime(encryptWorkTime(clubCustomerServiceInfo));
        genSecCustomerServiceInfoVO.setGenSecCustomerServiceGreetVOList(getCustomerServiceGreet(clubCustomerServiceInfo));
        return genSecCustomerServiceInfoVO;
    }

    public static String encryptWorkTime(ClubCustomerServiceInfo clubCustomerServiceInfo) {

        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("HH");
        String startTime = sdf.format(clubCustomerServiceInfo.getWorkStartTime());
        String endTime = sdf.format(clubCustomerServiceInfo.getWorkEndTime());
        String workDate = String.format("%s%s:00至%s:00", clubCustomerServiceInfo.getWorkType().getMsg(),startTime,endTime);
        return workDate;
    }

    public static List<GenSecCustomerServiceGreetVO> getCustomerServiceGreet(ClubCustomerServiceInfo clubCustomerServiceInfo) {
        if (StringUtils.isEmpty(clubCustomerServiceInfo.getCustomerServiceGreet())) {
            return null;
        }
        JSONArray jsonArray = JSONArray.parseArray(clubCustomerServiceInfo.getCustomerServiceGreet());
        List<GenSecCustomerServiceGreetVO> genSecCustomerServiceGreetVOList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String joinMsg = jsonObject.getString("msg");
            joinMsg = String.format(joinMsg,clubCustomerServiceInfo.getCsaName());
            GenSecCustomerServiceGreetVO genSecCustomerServiceGreetVO= new GenSecCustomerServiceGreetVO();
            genSecCustomerServiceGreetVO.setCusotmerServiceGreet(joinMsg);
            genSecCustomerServiceGreetVO.setChatHeadImage(clubCustomerServiceInfo.getHeadImage());
            genSecCustomerServiceGreetVOList.add(genSecCustomerServiceGreetVO);

        }
        return genSecCustomerServiceGreetVOList;


    }

}
